package com.len.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 动态添加数据源
 *
 * 监听 ContextRefreshedEvent 事件
 */
public class DataSourceInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlexDataSource dataSource = FlexGlobalConfig.getDefaultConfig()
                .getDataSource();

        HikariDataSource newDataSource = new HikariDataSource();
        newDataSource.setJdbcUrl("xxxx");
        newDataSource.setUsername("xxxx");
        newDataSource.setPassword("xxx");
        dataSource.addDataSource("newDataSource", newDataSource);
    }

}
